package com.sci.web.service.gateway.v2;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Page;
import com.sci.web.dao.BlogDao;
import com.sci.web.model.genmodel.Blog;
import com.sci.web.model.genmodel.User;
import com.sci.web.util.DateUtil;
import com.sci.web.util.StringUtil;

public class BlogService {
	
	
	
	
	/**
	 * @param pageNumber
	 * @return
	 */
	public Page<Blog> getBlogPageInate(int pageNumber){
		int pageSize = 6;
		return BlogDao.me.geetBlogPageInate(pageNumber, pageSize);
	}
	
	
	
	/**
	 * @param reqStr
	 * @param user
	 * @return
	 */
	public int insertBlog(Map<String , String> map , User user){
		int result = 0;
		//Map<String , String> map =(Map<String , String>) JSONObject.parseObject(reqStr, Map.class);
		String serialId = map.get("serialId");
		Blog geetBlogById = BlogDao.me.geetBlogById(serialId);
		if(geetBlogById==null){
			Blog blog = new Blog();
			blog.setTitle(map.get("title"));
			blog.setContent(map.get("content"));
			blog.setPushState(map.get("pushState"));
			blog.setUserNo(user.getUserNo());
			blog.setUserName(user.getUserName());
			String currDatatTime = DateUtil.getCurrDatatTime(DateUtil.DATA_FORMAT_1);
			blog.setUpdateTime(currDatatTime);
			if(StringUtil.isNotEmpty(blog.getPushState()) && "1".equals(blog.getPushState()) ){
				blog.setPushTime(currDatatTime);
			}
			blog.setBlogType(map.get("type"));
			blog.setSerialId(serialId);
			result =  BlogDao.me.insertBlog(blog);
		}else{
			result = 2;
		}
		
		
		return result;
	}
	
	
	
	public Blog getBlogInfo(String id){
		return BlogDao.me.geetBlogById(id);
	}
	
	
	public int updateBlog(Blog blog){
		return BlogDao.me.updateBlog(blog);
	}
	
	 public void updateBlogClickNum(String id , String clickNum){
		 Blog blogInfo = this.getBlogInfo(id);
		 blogInfo.setClickNum(String.valueOf(StringUtil.isEmpty(clickNum)?0:Integer.parseInt(clickNum)+1));
		 this.updateBlog(blogInfo);
	 }

}
